package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.example.permissionsystem1_1.businessFunction.dao.entity.po.*;
import com.example.permissionsystem1_1.businessFunction.dao.repository.*;
import com.example.permissionsystem1_1.businessFunction.service.converter.RoleFunctionServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.converter.UserRoleServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.roleFunctionReqModel.RoleFunctionSelectAllFunctionsByRoleIdReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.roleFunctionReqModel.RoleFunctionSetRoleFunctionRelationReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.userRoleReqModel.UserRoleSelectAllRolesByUserIdReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.userRoleReqModel.UserRoleSetUserRoleRelationReqModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.roleFunctionRspModel.RoleFunctionSelectAllFunctionsByRoleIdRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.roleFunctionRspModel.RoleFunctionSetRoleFunctionRelationRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.userRoleRspModel.UserRoleSelectAllRolesByUserIdRspModel;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.userRoleRspModel.UserRoleSetUserRoleRelationRspModel;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.RoleFunctionService;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.UserRoleService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import com.example.permissionsystem1_1.system.annotation.aspectAnnotation.SuperManageOperationAspectAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleFunctionServiceImpl extends BaseServiceImpl implements RoleFunctionService {
    @Autowired
    private RoleFunctionDao roleFunctionDao;
    @Autowired
    private RoleInfoDao roleInfoDao;
    @Autowired
    private FunctionInfoDao functionInfoDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserFunctionRecordDao userFunctionRecordDao;

    @Override
    @SuperManageOperationAspectAnnotation
    public RoleFunctionSelectAllFunctionsByRoleIdRspModel selectAllFunctionsByRoleId(RoleFunctionSelectAllFunctionsByRoleIdReqModel reqModel) {
        RoleFunctionPo po= RoleFunctionServiceConverter.convertReqModelToPo(reqModel);
        List<RoleFunctionPo> pos=null;
        if(po!=null&&po.getRoleId()!=null){
            pos=roleFunctionDao.selectAllFunctionsByRoleId(po.getRoleId());
        }
        return RoleFunctionServiceConverter.covertToSelectAllRolesByRoleIdModel(pos);
    }

    @Override
    @Transactional
    @SuperManageOperationAspectAnnotation
    public RoleFunctionSetRoleFunctionRelationRspModel setRoleFunctionRelation(RoleFunctionSetRoleFunctionRelationReqModel reqModel) {
        List<RoleFunctionPo> pos=RoleFunctionServiceConverter.convertReqModelToPo(reqModel);
        if(pos==null||pos.size()<=0){
            return null;
        }
        Long roleId=pos.get(0).getRoleId();
        RoleFunctionPo roleFunctionPo=new RoleFunctionPo();
        roleFunctionPo.setRoleId(roleId);
        int i1=roleFunctionDao.deletes(roleFunctionPo);//无论是否有这个角色,都进行一下数据清理,以便保证
        RoleInfoPo roleInfoPo=roleInfoDao.selectById(roleId);
        List<FunctionInfoPo> functionInfoPos=functionInfoDao.selectBatchByIdsAndState(reqModel.getFunctionIds(),"0");//只取启用的功能
        if(roleInfoPo==null||functionInfoPos==null||functionInfoPos.size()<=0){
            //仅仅做了删除原始关联,而没有对指定用户添加任何角色
            return RoleFunctionServiceConverter.covertToSetRoleFunctionRelationModel(i1);
        }
//        List<Long> functionIds=new ArrayList<>();//这里存的是查询后此时真实存在的功能
        List<RoleFunctionPo> existPos=new ArrayList<>();//因为可能存在对象数量的减少,所以需要另外创建一个存对象的集合才行
        Date now=new Date();
        for (FunctionInfoPo functionInfoPo:functionInfoPos) {
            Long functionId=functionInfoPo.getId();
            RoleFunctionPo existPo=new RoleFunctionPo();
            existPo.setRoleId(roleId);
            existPo.setFunctionId(functionId);
            existPo.setState("0");
            existPo.setCreator("admin");
            existPo.setCreateTime(now);
            existPo.setUpdator("admin");
            existPo.setUpdateTime(now);
            existPos.add(existPo);
        }
        int i=-1;
        int i2=roleFunctionDao.insertBatch(existPos);
        i=i1+i2;
        //功能权限的重建:根据角色id重建角色下的每个用户的全部用户功能记录
        reconstructionUserFunctionsByRoleId(roleId);
        return RoleFunctionServiceConverter.covertToSetRoleFunctionRelationModel(i);
    }
    //**********本类的业务处理方法
    /** 功能权限的重建:根据角色id重建角色下的每个用户的全部用户功能记录 */
    private void reconstructionUserFunctionsByRoleId(Long roleId){
        List<UserRolePo> userRolePos=userRoleDao.selectAllUsersByRoleId(roleId);
        if(userRolePos==null||userRolePos.size()<=0){
            return;
        }
        //对当前角色关联的所有用户,进行功能权限重建
        for (UserRolePo userRolePo:userRolePos) {
            Long userId=userRolePo.getUserId();
            UserFunctionRecordPo userFunctionRecordPo=new UserFunctionRecordPo();
            userFunctionRecordPo.setUserId(userId);
            userFunctionRecordDao.deletes(userFunctionRecordPo);
            List<FunctionInfoPo> functionInfoPos=functionInfoDao.selectsByUserId(userId);
            if(functionInfoPos==null||functionInfoPos.size()<=0){//这个用户没任何功能权限,所以进行下一个用户的功能权限构建
                continue;
            }
            List<UserFunctionRecordPo> userFunctionRecordPos=new ArrayList<>();
            String creator="admin";
            String updator="admin";
            Date now=new Date();
            for (FunctionInfoPo functionInfoPo:functionInfoPos) {
                UserFunctionRecordPo ufrPo=new UserFunctionRecordPo();
                ufrPo.setUserId(userId);
                ufrPo.setFunctionId(functionInfoPo.getId());
                ufrPo.setCreator(creator);
                ufrPo.setCreateTime(now);
                ufrPo.setUpdator(updator);
                ufrPo.setUpdateTime(now);
                userFunctionRecordPos.add(ufrPo);
            }
            userFunctionRecordDao.insertBatch(userFunctionRecordPos);
        }
    }
}
